package com.example.controller;



import com.example.entity.User;
import com.example.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


@Controller
public class LoginController {
    @RequestMapping("/login_input")
    public String loginInput(){
        return "login";
    }
    @RequestMapping("/login")
    public String login(String username, String password, Model model,HttpSession session,RedirectAttributes redirectAttributes){
        if ("admin".equals(username) && "123".equals(password)) {
            model.addAttribute("username", username);
            session.setAttribute("login_user", username);
            return "login_suc";
        } else {
            redirectAttributes.addFlashAttribute("redirect_msg");
            return "redirect:login_fail";
        }
    }

    @Autowired
    UserService service;
    @RequestMapping("/loginDb")
    public String loginDb(User user, Model model, HttpSession session, RedirectAttributes redirectAttributes){
        boolean isSuc=service.login(user);
        if(isSuc){
            model.addAttribute("username",user.getUsername());
            session.setAttribute("login_user",user.getUsername());
            return "login_suc";
        }else{
            model.addAttribute("username");
            redirectAttributes.addFlashAttribute("redirect_msg");
            redirectAttributes.addFlashAttribute("flash_msg");

            return  "redirect:login_fail";
        }
    }
    @RequestMapping("login_fail")
    public String loginFail(){
        return "login_fail";
    }
}
